<template>
    <div class="login-container">
    <div class="login-form-wrap">
        <!-- logo区域 -->
        <div class="login-head">
            <div class="logo"></div>
        </div>
            <!-- 表单区域 -->
            <el-form ref="form" class="login-form" :rules="rules" :model="form">
            <el-form-item prop="mobile">
                <el-input v-model="form.mobile"></el-input>
            </el-form-item>
            <el-form-item prop="code">
                <el-input v-model="form.code"></el-input>
            </el-form-item>
            <el-form-item prop="isAgree">
                <el-checkbox v-model="form.isAgree" label="我已阅读并同意用户协议和隐私条款"></el-checkbox>
            </el-form-item>
            <!-- 登录按钮 -->
            <el-form-item>  <!-- 给登录按钮注册点击事件 !-->
                <el-button :loading="isLoading" @click.prevent="hLogin" type="primary" class="login-btn">登录</el-button>
            </el-form-item>
            </el-form>
    </div>
     </div>
</template>
<script>

export default {
  data () {
    return {
      // 创建校验规则
      rules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1\d{10}$/, message: '请输入正确的手机号', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\d{6}$/, message: '请输入正确的密码', trigger: 'blur' }
        ],
        isAgree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('你必须同意，才能登陆'))
              }
            },
            trigger: 'change'
          }
        ]
      },
      isLoading: false,
      form: {
        isAgree: true,
        mobile: '13911111111',
        code: '246810'
      }

    }
  },
  methods: {
    async doLogin () {
      // 1. 收集用户的信息
      const { mobile, code } = this.form
      // 2. axios发ajax请求
      this.isLoading = true // 正在请求
      try {
        const res = await this.$http({
          method: 'POST',
          url: 'mp/v1_0/authorizations',
          data: {
            mobile,
            code
          }
        })
        // -------------成功----------
        // 1. 提示
        this.$message.success('登陆成功')
        this.isLoading = false // 请求结束
        // 2. 把token保存到本地
        localStorage.setItem('tokenStr', res.data.data.token)
        // 3. 跳转到主页
        this.$router.push('/')
      } catch (err) {
        console.log(err)
        // alert('登陆失败')
        // eslint-disable-next-line no-proto
        // this.__proto__.__proto__.$message.error('错了哦，这是一条错误消息')
        this.$message.error('错了哦，这是一条错误消息')
        this.isLoading = false // 请求结束
      }
    },
    doLogin_ () {
      //  获取表单数据
      const { mobile, code } = this.form
      // if (mobile === '' || code === '') {
      //   return
      // }
      // 通过axios请求登录接口
      this.isLoading = true
      this.$http({
        method: 'POST',
        url: '/mp/v1_0/authorizations',
        data: {
          mobile,
          code
        }
      }).then(res => {
        // 1.提示
        this.isLoading = false
        this.$message.success('登录成功')
        // 保存token到本地
        localStorage.setItem('tokenStr', res.data.data.token)
        // 2.跳转到主页
        this.$router.push('/')
        // alert('登录成功')
      }).catch(err => {
        console.log(err)
        // alert('请求失败')
        this.isLoading = false
        // 引入EmelentUI时，给vue原型添加$Message方法
        this.$message.error('登录失败')
      })
    },
    // 处理后端响应请求
    hLogin () {
    // this.$refs.form是用来获取对表单组件的引用
    // 在表单组件有一个validate函数，它的实参是一个回调函数
      this.$refs.form.validate(valid => {
        if (valid) {
          this.doLogin()
        }
      })
    }
  }

}

</script>
<style scoped lang="less">
// 铺满整屏
.login-container {
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction:column;
  justify-content: center;
  align-items: center;
  background: url(../../assets/login_bg.jpg) no-repeat;
  background-size: cover;
  .login-form-wrap {
    min-width: 300px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("../../assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-form {
      .login-btn {
        width: 100%;
      }
    }
  }
}
</style>
